package top.huiwow.items;

import org.bukkit.*;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.util.Vector;
import top.huiwow.GameCore;
import top.huiwow.managers.GamePlayer;
import top.huiwow.utils.*;

import java.text.NumberFormat;
import java.util.List;

public class Hyperion extends AkaishiBaseItem {
    GameCore plugin;
    public Hyperion() {
        type = Material.IRON_SWORD;
        name = "Hyperion";
        rarity = Rarity.LEGENDARY;
        internalName = "hype";
        itemType = ItemType.Weapon;
        shiny = true;
        ItemStats is = new ItemStats().setDamage(260).setStrength(150).setIntelligence(350);
        itemStats = is;
        extraLore = "NIGGER";
        plugin=GameCore.getInstance();
    }

    @EventHandler
    public void onHyperion(PlayerInteractEvent e) {
        Player p = e.getPlayer();
        if (!e.getAction().name().contains("RIGHT")) return;
        if (p.getItemInHand() == null || p.getItemInHand().getType() == Material.AIR) return;
        if (ItemBuilder.isItem(e.getItem(), this.internalName)) {
            triggerHypeAbility(p);
        }

    }
    public void triggerHypeAbility(Player player) {
        GamePlayer gamePlayer = GameCore.getInstance().getGamePlayerManager().get(player.getUniqueId());
        if (gamePlayer.getMana() < 50) {
            ActionBar.send(player, ChatColor.BOLD + "NOT ENOUGH MANA.");
            if (!plugin.sentNotEnoughMana.contains(player)) {
                plugin.sentNotEnoughMana.add(player);
                Bukkit.getScheduler().runTaskLater(plugin, new BukkitRunnable() {
                    @Override
                    public void run() {
                        plugin.sentNotEnoughMana.remove(player);
                    }
                }, 20);
            }
            return;
        }
        //////
        double baseDamage = 5000;
        //////
        double totalDamage = 0;
        plugin.getGamePlayerManager().get(player.getUniqueId()).reduceMana(50);
        player.setVelocity(player.getVelocity().setY(0));
        if (player.getEyeLocation().add(0, 1, 0).getBlock().isEmpty() || player.getEyeLocation().add(0, 1, 0).getBlock().isLiquid()) {
            player.teleport(player.getLocation().add(0, 1, 0));
        }
        ItemUtils.teleport(player);
        player.addPotionEffect(new PotionEffect(PotionEffectType.ABSORPTION, 60, 5), true);
        player.getWorld().playSound(player.getLocation(), Sound.ZOMBIE_REMEDY, 0.2f, 2f);
        Particle.play(player.getLocation().add(0.0, 0.5, 0.0), Effect.EXPLOSION_LARGE);
        List<LivingEntity> close = WorldUtils.getNearbyMobs(player, 6);
        for (LivingEntity target : close) {
            Vector velocity = target.getVelocity();
            double finalDamage = DamageCalculator.calculateAbilityDamage(baseDamage, gamePlayer.getIntelligence(), 1, gamePlayer.getCritChance(), gamePlayer.getCritDamage(), 0, 0);
            EntityDamageByEntityEvent event = new EntityDamageByEntityEvent(player, target, EntityDamageEvent.DamageCause.ABILITY_DAMAGE, 15);
            Bukkit.getPluginManager().callEvent(event);
            if (!event.isCancelled()) {
                target.damage(finalDamage);
                totalDamage += finalDamage;
            }
            target.setVelocity(velocity);//so that using this ability won't change the targets' velocity


        }
        if (close.size() != 0) {
            player.sendMessage("Your " + ChatColor.RED + "Implosion " + ChatColor.RESET + "hit " + ChatColor.RED + close.size() + ChatColor.RESET + " enemy for " + ChatColor.RED + NumberFormat.getInstance().format(totalDamage) + ChatColor.RESET + " damage.");
        }
        player.getWorld().playSound(player.getLocation(), Sound.EXPLODE, 0.4f, 1.2f);
    }
}
